package ArrayList.Dao;

import ArrayList.DBLink.AddressList;
import ArrayList.Swing.AddressListSwing;

import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AddressListDaoImpl extends AddressListSwing implements AddressListDao {
    //业务逻辑层
    Connection con = null;
    ResultSet rs = null;
    @Override
    public void query(Object[][] a) {
        int i = 0;
        while (i < 37) {
            a[i][0] = " ";
            a[i][1] = " ";
            a[i][2] = " ";
            a[i][3] = " ";
            i++;
        }
        i = 0;
        con = AddressList.connectDB("arraylist", "root", "451680");
        try {
            rs = AddressList.query(con);
            while (rs.next()) {
                int contactNumber = rs.getInt(1);
                String contactName = rs.getString(2);
                String contactPhone = rs.getString(3);
                String contactAddress = rs.getString(4);
                a[i][0] = contactNumber;
                a[i][1] = contactName;
                a[i][2] = contactPhone;
                a[i][3] = contactAddress;
                i++;
            }
            repaint();
            try {
                con.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
    @Override
    public void add(String name,String phone,String address) {
        int key;
        int point = 0;
        if (address.equals("")
                || phone.equals("") || name.equals("")
        ) {
            JOptionPane.showMessageDialog(null, "提示：添加时选项不能为空，请重新填写！");
        } else {
            try {
                con = AddressList.connectDB("arraylist", "root", "451680");
                rs = AddressList.query_add(con);
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
            try {
                if (rs.next()) {
                    key = rs.getInt(1);
                    point = key + 1;
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            try {
                con = AddressList.connectDB("arraylist", "root", "451680");
                AddressList.add(con,point,name,phone,address);
                JOptionPane.showMessageDialog(null, "添加成功，请点击查询查看");
            } catch (SQLException e2) {
                e2.printStackTrace();
                System.out.println("数据库连接失败3");
            }
            try {
                con.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }
    @Override
    public void update(String id,String name,String phone,String address) {
        int key2 = 0;
        int num2=0, num3 = 0;
        if (id.equals("") || phone.equals("") || address.equals("")
                || name.equals("") ) {
            JOptionPane.showMessageDialog(null, "提示：修改时所有选项不能为空，请重新填写！");
        } else {
            try {
                num2 = Integer.parseInt(id);
                con = AddressList.connectDB("arraylist", "root", "451680");
                rs = AddressList.query_update(con,num2);
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
            try {
                if (rs.next()) {
                    num3 = rs.getInt(1);
                    key2 = 1;
                } else {
                    JOptionPane.showMessageDialog(null, "提示：系统不存在此联系人信息，请重新填写！");
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            if (key2 == 1) {
                key2 = 0;
                try {
                    con = AddressList.connectDB("arraylist", "root", "451680");
                    AddressList.update(con,num2,name,phone,address,num3);
                    JOptionPane.showMessageDialog(null, "修改成功，请刷新后查看");

                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
                try {
                    con.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }

    }
    @Override
    public void delete(String id) {
        int num = 0, key1 = 0;
        if (id.equals("")) {
            JOptionPane.showMessageDialog(null, "提示：删除操作时联系人编号选项不能为空，请重新填写！");
        } else {
            int num1 = Integer.parseInt(id);
            try {
                con = AddressList.connectDB("arraylist", "root", "451680");
                rs = AddressList.query_delete(con,num1);
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
            try {
                if (rs.next()) {
                    num = rs.getInt(1);
                    key1 = 1;
                } else {
                    JOptionPane.showMessageDialog(null, "提示：系统不存在此联系人编号，请重新填写！");
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            if (key1 == 1) {
                key1 = 0;
                try {
                    con = AddressList.connectDB("arraylist", "root", "451680");
                    int a = AddressList.delete(con,num);
                    JOptionPane.showMessageDialog(null, "删除成功！");
                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
                try {
                    con.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
